<template>
  <el-dialog
    title="提示"
    :visible="showDialog"
    width="50%"
    center
    @close="closeFn"
  >
    <el-form :model="formdata">
      <el-form-item label="经纪" label-width="120px">
        <el-select v-model="formdata.agent" filterable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" label-width="120px">
        <el-input v-model="formdata.name" />
      </el-form-item>
      <el-form-item label="邮箱" label-width="120px">
        <el-input v-model="formdata.email" />
      </el-form-item>
      <el-form-item label="电话" label-width="120px">
        <el-input v-model="formdata.phone" />
      </el-form-item>
      <el-form-item label="微信" label-width="120px">
        <el-input v-model="formdata.wechat" />
      </el-form-item>
      <el-form-item label="描述" label-width="120px">
        <el-input v-model="formdata.desc" />
      </el-form-item>
      <el-form-item label="图片" label-width="120px">
        <el-upload
          class="avatar-uploader"
          :action="imageUrl"
          :show-file-list="false"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <el-input v-model="formdata.image" placeholder="强制设置图片跳过上传" />
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="add">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addfromdata, editdata, getagents } from '@/api/clientManagement'
// import COS from 'cos-js-sdk-v5'
// var cos = new COS({
//   SecretId: 'AKID4xcmfHchW4LN8cpmSrzqRloa6MK1ScV7',
//   SecretKey: 'MSIr9BwQecJjskMhlY5MM5YGiuqVt0qQ'
// })
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formdata: {
        agent: '', // id 值
        name: '', // 姓名
        email: '', // 电子邮箱
        phone: '', // 手机号
        wechat: '', // 微信
        desc: '', // 测试用户
        image: ''
      },
      imageUrl: 'https://ts2.cn.mm.bing.net/th?id=ORMS.498942724b186814d4cea5f49fa12b3b&pid=Wdp&w=300&h=156&qlt=90&c=1&rs=1&dpr=1&p=0',
      options: [],
      value: ''
    }
  },
  async created() {
    const res = await getagents()
    this.options = res.data
  },
  methods: {
    closeFn() {
      this.$emit('update:showDialog', false)
    },
    async add() {
      console.log(this.formdata)
      if (this.formdata.id) {
        await editdata(this.formdata)
        console.log(this.formdata)
      } else {
        // console.log(this.formdata)
        const res = await addfromdata(this.formdata)
        // const uploadImg = this.$refs.uploadIm1
        // this.userInfo.image = uploadImg.fileList[0] && uploadImg.fileList[0].url
        console.log(res)
      }
      this.$emit('transcation')
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style lang='scss' scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
